package com.answer.service.impl;

import cn.hutool.json.JSONUtil;
import com.answer.mapper.ScoringResultMapper;
import com.answer.model.dto.ScoringResultDto.ScoringResultAddDTO;
import com.answer.model.dto.ScoringResultDto.ScoringResultEditDTO;
import com.answer.model.dto.ScoringResultDto.ScoringResultQueryDTO;
import com.answer.model.dto.ScoringResultDto.ScoringResultUpdateDTO;
import com.answer.model.entity.ScoringResult;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.ScoringResultVo;
import com.answer.service.ScoringResultService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【scoring_result(评分结果)】的数据库操作Service实现
* @createDate 2024-06-29 15:23:08
*/
@Service
public class ScoringResultServiceImpl extends ServiceImpl<ScoringResultMapper, ScoringResult>
    implements ScoringResultService{


    @Override
    public Boolean addScoringResult(ScoringResultAddDTO scoringResultAddDTO) {

        ScoringResult ScoringResult = new ScoringResult();
        BeanUtils.copyProperties(scoringResultAddDTO, ScoringResult);
        List<String> resultProp = scoringResultAddDTO.getResultProp();
        ScoringResult.setResultProp(JSONUtil.toJsonStr(resultProp));
        int insert = baseMapper.insert(ScoringResult);
        ScoringResultVo scoringResultVo = new ScoringResultVo();
        BeanUtils.copyProperties(ScoringResult, scoringResultVo);
        return insert==1;
    }

    @Override
    public ResultPage<ScoringResultVo> queryAll(ScoringResultQueryDTO scoringResultQueryDTO) {
        Page page = new Page(scoringResultQueryDTO.getCurrent(), scoringResultQueryDTO.getSize());
        baseMapper.selectPage(page, getQueryWrapper(scoringResultQueryDTO));
        List<ScoringResult> scoringResults = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long total = page.getTotal();
        List<ScoringResultVo> scoringResultVoList = new ArrayList<>();
        for (ScoringResult ScoringResult : scoringResults) {
            ScoringResultVo scoringResultVo = new ScoringResultVo();
            BeanUtils.copyProperties(ScoringResult, scoringResultVo);
            String resultProp = ScoringResult.getResultProp();
            scoringResultVo.setResultProp(JSONUtil.toList(resultProp, String.class));
            scoringResultVoList.add(scoringResultVo);
        }

        ResultPage resultPage = new ResultPage();
        resultPage.setRecords(scoringResultVoList);
        resultPage.setCurrent(current);
        resultPage.setPages(pages);
        resultPage.setTotal(total);
        return resultPage;
    }

    @Override
    public ScoringResultVo queryOneById(Long id) {

        ScoringResult ScoringResult = baseMapper.selectById(id);
        ScoringResultVo scoringResultVo = new ScoringResultVo();
        BeanUtils.copyProperties(ScoringResult, scoringResultVo);
        String resultProp = ScoringResult.getResultProp();
        scoringResultVo.setResultProp(JSONUtil.toList(resultProp, String.class));
        return scoringResultVo;
    }

    @Override
    public Boolean updateScoringResult(ScoringResultUpdateDTO scoringResultUpdateDTO) {
        ScoringResult ScoringResult = new ScoringResult();
        BeanUtils.copyProperties(scoringResultUpdateDTO, ScoringResult);
        List<String> resultProp = scoringResultUpdateDTO.getResultProp();
        ScoringResult.setResultProp(JSONUtil.toJsonStr(resultProp));
        int i = baseMapper.updateById(ScoringResult);
        return i == 1;
    }

    @Override
    public Boolean editScoringResult(ScoringResultEditDTO scoringResultEditDTO) {
        ScoringResult ScoringResult = new ScoringResult();
        BeanUtils.copyProperties(scoringResultEditDTO, ScoringResult);
        int i = baseMapper.updateById(ScoringResult);
        return i == 1;
    }


    private LambdaQueryWrapper<ScoringResult> getQueryWrapper(ScoringResultQueryDTO scoringResultQueryDTO) {
        LambdaQueryWrapper<ScoringResult> queryWrapper = new LambdaQueryWrapper<>();
        return queryWrapper;
    }



}




